<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input v-model="skuParams.skuName" placeholder="SKU名称"></el-input>
    </el-form-item>

    <el-form-item label="价格(元)">
      <el-input v-model="skuParams.price" placeholder="价格(元)" type="number"></el-input>
    </el-form-item>

    <el-form-item label="重量(g)">
      <el-input v-model="skuParams.weight" placeholder="重量(g)"></el-input>
    </el-form-item>

    <el-form-item label="SKU描述">
      <el-input v-model="skuParams.skuDesc" placeholder="SKU描述" type="textarea"></el-input>
    </el-form-item>

    <el-form-item label="平台属性">
      <!--内层form-->
      <el-form inline>
        <el-form-item v-for="(item) in attrArr" :key="item.id" :label="item.attrName">
          <el-select style="width: 200px" v-model="item.attrIdAndValueId">
            <el-option :value="`${item.id}:${attrValue.id}`" v-for="(attrValue) in item.attrValueList"
                       :key="attrValue.id"
                       :label="attrValue.valueName"/>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>

    <el-form-item label="销售属性">
      <el-form inline>
        <el-form-item v-for="(item) in saleArr" :key="item.id" :label="item.saleAttrName">
          <el-select style="width: 200px" v-model="item.saleIdAndValueId">
            <el-option :value="`${item.id}:${attrItem.id}`" v-for="(attrItem) in item.spuSaleAttrValueList"
                       :key="attrItem.id"
                       :label="attrItem.saleAttrValueName"/>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>

    <el-form-item label="图片名称">
      <el-table stripe border :data="imgArr" ref="ImgTableVc">
        <el-table-column type="selection" width="80px" align="center"></el-table-column>
        <el-table-column label="图片">
          <template #="{row}">
            <img :src="row.imgUrl" style="width: 100px;height: 100px" alt="">
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #="{row}">
            <el-button type="primary" @click="handler(row)">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="danger" @click="cancel">取消</el-button>
    </el-form-item>

  </el-form>


</template>

<script setup lang="ts">
//@ts-ignore
import CategoryStore from "@/piniaStore/moudules/categoryStore.ts";
//@ts-ignore
import type {T_SpuData, T_SpuImageResponse, T_SaleAttrResponseData, T_SkuData} from "@/api/product/spu/type.ts";
//@ts-ignore
import {reqSpuImageList, reqSpuHasSaleAttr, reqAddSku} from "@/api/product/spu/spuApi.ts";
import {reactive, ref, onBeforeUnmount} from "vue";
import {ElMessage} from "element-plus";


//自定义事件
let $emit = defineEmits(['changeScene']);
//拿到分类仓库
const categoryStore = CategoryStore();
//存储平台属性
let attrArr = ref<any>([]);
//销售属性
let saleArr = ref<any>([]);
//照片墙
let imgArr = ref<any>([]);
//收集Sku的参数
let skuParams = reactive<T_SkuData>({
  category3Id: "",
  spuId: "",
  tmId: "",
  //v-model收集
  skuName: "",
  price: "",
  weight: "",
  skuDesc: "",
  skuDefaultImg: "",
  skuAttrValueList: [],
  skuSaleAttrValueList: []
});
//照片墙列表VC实例
let ImgTableVc = ref<any>();

//取消按钮的回调
const cancel = () => {
  $emit('changeScene', {scene: 0, params: ""})
}

const initSkuData = async (spu: T_SpuData) => {
  //收集数据
  skuParams.category3Id = spu.category3Id;
  skuParams.spuId = spu.id;
  skuParams.tmId = spu.tmId;
  //发请求获取数据
  //获取平台属性
  let resultData: any = await categoryStore.getAttrs(spu.id);
  //获取照片墙
  let resultImgList: T_SpuImageResponse = await reqSpuImageList(spu.id);
  //获取销售属性
  let resultHasSaleAttr: T_SaleAttrResponseData = await reqSpuHasSaleAttr(spu.id);
  console.log(resultData);
  console.log(resultHasSaleAttr.data);
  console.log(resultImgList.data);
  //存取数据
  attrArr.value = resultData;
  saleArr.value = resultHasSaleAttr.data;
  imgArr.value = resultImgList.data;


}

//图片设为默认值的回调
const handler = (row: any) => {
  //先清楚所有的复选框
  // imgArr.value.forEach((item:any)=>{
  //   ImgTableVc.value.toggleRowSelection(item,false)
  // })
  ImgTableVc.value.clearSelection();
  //再勾选选择复选框
  ImgTableVc.value.toggleRowSelection(row, true);
  skuParams.skuDefaultImg = row.imgUrl;
}

//保存按钮的回调
const save = async () => {
  //整理参数
  //平台属性
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':');
      prev.push({
        attrId,
        valueId
      })
    }
    return prev;
  }, []);
  //销售属性
  skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':');
      prev.push({
        saleAttrId, saleAttrValueId
      })
    }
    return prev;
  }, [])

  //发送请求:添加sku请求
  let result: any = await reqAddSku(skuParams);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: "添加sku成功"
    })
    //返回场景0
    $emit('changeScene', {scene: 0, params: ""})
  } else {
    ElMessage({
      type: "error",
      message: "添加sku失败"
    })
  }
}

//路由组件销毁前，清空仓库数据
onBeforeUnmount(() => {
  categoryStore.$reset();
})


//子组件的方法对外暴露
defineExpose({
  initSkuData
})

</script>

<style scoped>

</style>